package com.winit.usTask.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.common.rabbitSend.MsgSend;
import com.winit.commons.util.SeqGenerate;
import com.winit.usTask.service.UsTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * Created by wanglin on 2018/6/4.
 */
@Component
@Api(value = "usTask", tags = {"usTask"}, description = "usTask")
@RestController
@RequestMapping("/api/v1/usTask")
public class UsTaskController {
    @Autowired
    UsTaskService usTaskService;
    @Autowired
    MsgSend msgSend;

    @RequestMapping(value = "/runUsTask", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "执行定时消息")
    @ResponseBody
    public ExecuteResult runUsTask(
            @ApiParam(value = "ip",required = true) @RequestParam(value = "ip", required = true) String ip,
            @ApiParam(value = "post",required = true) @RequestParam(value = "post", required = true) String post,
            @ApiParam(value = "sys",required = true) @RequestParam(value = "sys", required = true) String sys,
            @ApiParam(value = "taskName",required = true) @RequestParam(value = "taskName", required = true) String taskName,
            @ApiParam(value = "页面id",required = true) @RequestParam(value = "uId", required = true) String uId)
    {
        return usTaskService.runUsTask(ip,post,sys,taskName,uId);
    }

    @RequestMapping(value = "/getSys", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取分组/工程")
    @ResponseBody
    public ExecuteResult getSys(
            @ApiParam(value = "任务类型",required = true) @RequestParam(value = "type", required = true) String type
            ){
        return usTaskService.getGroups(type);
    }

    @RequestMapping(value = "/getTasks", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取任务信息")
    @ResponseBody
    public ExecuteResult getTasks(
            @ApiParam(value = "任务类型",required = true) @RequestParam(value = "type", required = true) String type,
            @ApiParam(value = "分组名称",required = true) @RequestParam(value = "sys", required = true) String sys
    ){
        return usTaskService.getTasks(type,sys);
    }

    @RequestMapping(value = "/uscheduleRtn", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取返回消息")
    @ResponseBody
    public void uscheduleRtn(
            @ApiParam(value = "id",required = true) @RequestParam(value = "id", required = true) String id,
            @ApiParam(value = "status",required = true) @RequestParam(value = "status", required = true) String status
    ){
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("id",id);
                jsonObject.put("status",status);
        msgSend.sendMsg(jsonObject);
    }

    @RequestMapping(value = "/getUSNo", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取uschedule前端编号")
    @ResponseBody
    public ExecuteResult getUSNo(){
        ExecuteResult er=new ExecuteResult();
        er.setData("US"+ SeqGenerate.createSerialNo());
        return er;
    }


    /**
     * 定时任务扫描uschedule数据
     * @throws ParseException
     */
    @Scheduled(cron="0/60 * * * * ?")
    public void timingUpdateClosed()  throws ParseException {
        usTaskService.initUschedule();
    }
}
